package loader.core.load
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import loader.core.BaseServer;
	import loader.core.LoadFile;
	import loader.core.LoadType;
	/**
	 * 资源加载器 
	 * @author chenanbao
	 * 
	 */	
	public class ResLoader extends BaseLoader
	{
		private var urlLoader:URLLoader;
		
		public function ResLoader(loadServer:BaseServer)
		{
			super(loadServer);
			urlLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
		}
		
		public override function initListener():void{
			urlLoader.addEventListener(Event.COMPLETE,completeHandler);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			if(this.server.proessHandler != null){
				urlLoader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			}
		}
		
		public override function  removeListener():void{
			urlLoader.removeEventListener(Event.COMPLETE,completeHandler);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			urlLoader.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
		}
		
		public function progressHandler(evt:ProgressEvent):void{
			this.server.proessHandler(this.file.fileName,evt.bytesLoaded,evt.bytesTotal);
		}
		
		public override  function loadFile(file:LoadFile):void{
			super.loadFile(file);
			urlLoader.load(new URLRequest(file.loadUrl));
		}
		
		public override function completeHandler(event:Event):void{
			this.file.data = urlLoader.data;
			super.completeHandler(event);
		}
		//清除资源
		public override function destory():void{
			super.destory();
			urlLoader.close();
		}

	}
}